package test1;

import java.util.*;
import java.io.FileInputStream;
import java.security.*;  
import java.security.cert.Certificate;  
  
public class ReadPFX {  
    public ReadPFX (){  
    }  
    
	public static String byte2hex(byte[] b) // 二行制转字符串
	{
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs + " ";
		}
		return hs.toUpperCase();
	}
    
    public static String byte2HexStr(byte[] b)    
    {    
        String stmp="";
        StringBuilder sb = new StringBuilder("");    
        for (int n=0;n<b.length;n++)    
        {    
            stmp = Integer.toHexString(b[n] & 0xFF);    
            sb.append((stmp.length()==1)? "0"+stmp : stmp);    
            sb.append(" ");    
        }
        return sb.toString().toUpperCase().trim();    
    }    
        
    
    public static String bytesToHexString(byte[] src){  
        StringBuilder stringBuilder = new StringBuilder("");  
        if (src == null || src.length <= 0) {  
            return null;  
        }  
        for (int i = 0; i < src.length; i++) {  
            int v = src[i] & 0xFF;  
            String hv = Integer.toHexString(v);  
            if (hv.length() < 2) {  
                stringBuilder.append(0);  
            }  
            stringBuilder.append(hv);
        }  
        return stringBuilder.toString();  
    }  
    
    //转换成十六进制字符串  
    public static String Byte2String(byte[] b) {
        String hs="";  
        String stmp="";  
   
        for (int n=0;n<b.length;n++) {  
            stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));  
            if (stmp.length()==1) hs=hs+"0"+stmp+"  ";  
            else hs=hs+stmp+" ";  
            //if (n<b.length-1)  hs=hs+":";  
        }  
        return hs.toUpperCase();  
    }  
      
    public static byte[] StringToByte(int number) {   
        int temp = number;   
        byte[] b=new byte[4];   
        for (int i=b.length-1;i>-1;i--){   
            b[i] = new Integer(temp&0xff).byteValue();//将最高位保存在最低位   
            temp = temp >> 8; //向右移8位   
        }   
        return b;   
    } 
     
    public  PrivateKey GetPvkformPfx(String strPfx, String strPassword){  
        try {  
            KeyStore ks = KeyStore.getInstance("PKCS12");  
            FileInputStream fis = new FileInputStream(strPfx);  
            // If the keystore password is empty(""), then we have to set  
            // to null, otherwise it won't work!!!  
            char[] nPassword = null;  
            if ((strPassword == null) || strPassword.trim().equals("")){  
                nPassword = null;  
            }  
            else  
            {  
                nPassword = strPassword.toCharArray();  
            }  
            ks.load(fis, nPassword);  
            fis.close();  
            System.out.println("keystore type=" + ks.getType());  
            // Now we loop all the aliases, we need the alias to get keys.  
            // It seems that this value is the "Friendly name" field in the  
            // detals tab <-- Certificate window <-- view <-- Certificate  
            // Button <-- Content tab <-- Internet Options <-- Tools menu   
            // In MS IE 6.  通过别名得到秘钥
            Enumeration enumas = ks.aliases();  
            String keyAlias = null;  
            if (enumas.hasMoreElements())// we are readin just one certificate.  
            {  
                keyAlias = (String)enumas.nextElement();   
                System.out.println("alias=[" + keyAlias + "]");  
            }  
            //现在，一旦我们知道了别名，我们可以得到的钥匙。
            System.out.println("is key entry=" + ks.isKeyEntry(keyAlias));  
            PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword); 
            Certificate cert = ks.getCertificate(keyAlias);  
            PublicKey pubkey = cert.getPublicKey();  
            
            System.out.println(byte2hex(pubkey.getEncoded()));

            return prikey;  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        return null;  
    }  
}  